<?php

class Fuel_consumption extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Fuel_consumption':
	 * @var integer $id
	 * @var string $type
	 * @var string $front_driving
	 * @var double $empty_driving
	 * @var double $light_driving
	 * @var double $heavy_driving
	 * @var integer $state
	 * @var string $create_time
	 */
	public $type;
	public $plate_no;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'fuel_consumption';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('plate_no','length','max'=>10),
			array('type','length','max'=>20),
			array('front_driving,empty_driving,light_driving,heavy_driving', 'numerical'),
			array('plate_no,type,front_driving,empty_driving,light_driving,heavy_driving,effect_date,lapse_date', 'required'),
			array('front_driving,empty_driving,light_driving,heavy_driving',
			'match','pattern' => '/^\d{1,3}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多3位，小数位2位'),
			array('effect_date,lapse_date', 'match','pattern' => '/\d{4}-\d{2}$/','message'=>'{attribute} 必须为日期型(年-月)'),
		);
	}
	
	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)//在验证之前将无需用户输入的字段写入
	{
		if($this->isNewRecord){
			$this->state=0;
			$this->create_time=date('Y-m-d G:i:s');
		}
		return true;
	}

	protected function afterValidate($on)//在验证之前将无需用户输入的字段写入
	{	
		if($this->hasErrors()) return;
		$this->effect_date.='-01';
		$this->lapse_date.='-01';
		return true;
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '索引',
			'type'=>'车型',
			'plate_no' =>'车牌号',
			'fk_truck_id'=>'车辆id',
			'fk_type_id'=>'车型id',
			'front_driving' => '车头',
			'empty_driving' => '空驶',
			'light_driving' => '轻驶',
			'heavy_driving' => '重驶',
			'effect_date' => '生效日期',
			'lapse_date' => '失效日期',
			'state' => '状态',
			'create_time' => '创建时间',
		);
	}
	public function safeAttributes(){
		$params=parent::safeAttributes();
		$params[]='type';
		$params[]='plate_no';
		return $params;
	}
}